// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

namespace nebula {
namespace internal {

// ----------------------------------------------------------------------
// BEGIN Hash utilities from Boost

namespace detail {

#if defined(_MSC_VER)
#define NEBULA_HASH_ROTL32(x, r) _rotl(x, r)
#else
#define NEBULA_HASH_ROTL32(x, r) (x << r) | (x >> (32 - r))
#endif

template <typename SizeT>
inline void hash_combine_impl(SizeT& seed, SizeT value) {
  seed ^= value + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}

inline void hash_combine_impl(uint32_t& h1, uint32_t k1) {
  const uint32_t c1 = 0xcc9e2d51;
  const uint32_t c2 = 0x1b873593;

  k1 *= c1;
  k1 = NEBULA_HASH_ROTL32(k1, 15);
  k1 *= c2;

  h1 ^= k1;
  h1 = NEBULA_HASH_ROTL32(h1, 13);
  h1 = h1 * 5 + 0xe6546b64;
}

#undef NEBULA_HASH_ROTL32

}  // namespace detail

template <class T>
inline void hash_combine(std::size_t& seed, T const& v) {
  std::hash<T> hasher;
  return ::nebula::internal::detail::hash_combine_impl(seed, hasher(v));
}

// END Hash utilities from Boost
// ----------------------------------------------------------------------

}  // namespace internal
}  // namespace nebula
